<?php

namespace App\Models\Index;

use App\Models\User\LiveUser;
use App\Models\User\LiveUserFollow;
use Illuminate\Database\Eloquent\Model;

class LiveVideo extends Model
{
    public $table = 'live_video';
    public $timestamps = false;

    public function getTimeAttribute($val)
    {
        //计算分钟
        //算法：将秒数除以60，然后下舍入，既得到分钟数
        $h    =    floor($val/60);
        //计算秒
        //算法：取得秒%60的余数，既得到秒数
        $val  =    $val%60;
        //如果只有一位数，前面增加一个0
        $h    =    (strlen($h)==1)?'0'.$h:$h;
        $val  =    (strlen($val)==1)?'0'.$val:$val;
        return $h.':'.$val;
    }

    public function getCreatedTimeAttribute($val)
    {
        return date('Y-m-d',$val);
    }

    #获取热门视频列表
    public static function getHotVideoList(int $attribute, int $user_id)
    {
        $data = self::with(['user','follow' => function ($query) use ($user_id) {
                    $query->where('user_id','=',$user_id);
                }])->where('attribute','=',$attribute)->where('status','=',1)->orderByDesc('click_count')->paginate(15)->toArray();
        if (empty($data)) {
            return [];
        } else {
            return $data;
        }
    }

    #获取推荐视频列表
    public static function getRecommendVideoList(int $attribute)
    {
        $data = self::query()->where('attribute','=',$attribute)->where('status','=',1)->orderByDesc('click_count')->paginate(15)->toArray();
        if (empty($data)) {
            return [];
        } else {
            return $data;
        }
    }

    #获取指定的视频
    public static function getAppointVideo(int $id, int $user_id)
    {
        $data = self::with(['user','follow' => function ($query) use ($user_id) {
                    $query->where('user_id','=',$user_id);
                }])->find($id);
        if (empty($data)) {
            return [];
        } else {
            self::query()->where('id','=',$id)->increment('click_count',1);
            $data->click_count += 1;
            return $data;
        }
    }

    #根据用户ID获取视频列表
    public static function getVideoListByUserId(int $user_id)
    {
        $data = self::query()->where('user_id','=',$user_id)->where('status','=',1)->orderByDesc('created_time')->paginate(15)->toArray();
        if (empty($data)) {
            return [];
        } else {
            return $data;
        }
    }

    #关联用户表
    public function user()
    {
        return $this->hasOne(LiveUser::class,'id','user_id')->select('id','user_name','user_avatar')->withDefault([
            'user_name'     => '',
            'user_avatar'   => '',
        ]);
    }

    #关联关注表
    public function follow()
    {
        return $this->hasOne(LiveUserFollow::class,'to_user_id','user_id')->select('to_user_id','status')->withDefault([
            'status' => '0',
        ]);
    }

    public static function getVideoList($search)
    {
        $data = LiveVideo::query()
                ->join('live_user','live_user.id','=','live_video.user_id')
                ->where(function ($query) use($search) {
                    $query->when($search !== '' && $search !== null, function ($query) use($search) {
                        return $query->where('live_user.id', 'like', "%{$search}%")->orWhere('live_user.user_name', 'like', "%{$search}%");
                    });
                })
                ->select('live_user.id as user_id','live_user.user_name','live_user.user_avatar','live_user.user_sex','live_video.*')
                ->orderByDesc('live_video.click_count')
                ->paginate(25);
        return $data;
    }
}
